package com.mall.order.mq.delayordere;

/**
 * @author: lds
 * @clsss: DelayOrderConsumer:
 */

import com.mall.order.OrderCoreService;
import com.mall.order.dal.persistence.OrderItemMapper;
import com.mall.order.dal.persistence.OrderMapper;
import com.mall.order.dto.CancelOrderRequest;
import com.mall.order.dto.CancelOrderResponse;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.message.MessageExt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.nio.charset.Charset;
import java.util.List;

/**
 *  消费延迟订单的消费者：
 *  1.从获取到的消息中拿出订单编号
 *  2.查询tb_order表中对应订单的状态，如果订单状态是以支付，什么也不做，
 *  如果订单的状态不是已支付状态，此时取消订单
 *  a.修改订单状态
 *  b.还原商品的冻结库存
 *  c.修改订单条目的库存状态为已释放
 */
@Component
public class DelayOrderConsumer {


    private DefaultMQPushConsumer consumer;

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    OrderItemMapper orderItemMapper;
    @Reference
    OrderCoreService orderCoreService;

    @PostConstruct
    public void init() {
        consumer=new DefaultMQPushConsumer("delay_order_consumer");
        consumer.setNamesrvAddr("127.0.0.1:9876");
        try {
            consumer.subscribe("delay_order","*");
            consumer.setMessageListener(new MessageListenerConcurrently() {
                @Override
                public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> messageExtList, ConsumeConcurrentlyContext consumeConcurrentlyContext) {

                    //在该方法执行过程中如果出现了异常
                    try {
                        //实现消费逻辑
                        //这里一次只发送一个消息，所以一次只取一个消息
                        MessageExt message = messageExtList.get(0);

                        //获取消息体中封装的数据
                        byte[] body = message.getBody();
                        //安装消费逻辑处理获取到的数据
                        String orderId = new String(body, 0, body.length, Charset.forName("utf-8"));
                        System.out.println("messageId"+message.getMsgId());
                        System.out.println("接收到了消息"+orderId);
                        CancelOrderRequest cancelOrderRequest = new CancelOrderRequest();
                        cancelOrderRequest.setOrderId(orderId);
                        CancelOrderResponse cancelOrderResponse = orderCoreService.cancelOrder(cancelOrderRequest);
                        //取消订单业务逻辑
                        //1.从消息中取出订单id
                        //2.查询对应订单信息,如果订单已经支付什么也不要做
                        //3.如果订单状态是未支付状态，订单取消
                        // a.修改订单状态为已取消
                        // b.还原锁定库存
                        //重试16次如果成功则不会重试
                        return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
                    }catch (Exception e){
                        e.printStackTrace();
                        return ConsumeConcurrentlyStatus.RECONSUME_LATER;
                    }



                }
            });
            //启动
            consumer.start();
        } catch (MQClientException e) {
            e.printStackTrace();
        }

    }

}
